package com.wutongyu.mannyburger;

import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import androidx.core.app.NotificationCompat;

public class WifiStateReceiver extends BroadcastReceiver {
    private static final String TAG = "WifiStateReceiver";
    private static final String CHANNEL_ID = "wifi_notification_channel";
    private static final int NOTIFICATION_ID = 100;

    @Override
    public void onReceive(Context context, Intent intent) {
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
        if (wifiState == WifiManager.WIFI_STATE_DISABLED) {
            // 使用通知而非对话框，这样在任何上下文中都安全
            showWifiNotification(context);
        }
    }

    private void showWifiNotification(Context context) {
        try {
            // 创建通知渠道（Android 8.0及以上需要）
            createNotificationChannel(context);
            
            // 创建打开WiFi设置的Intent
            Intent wifiSettingsIntent = new Intent(Settings.ACTION_WIFI_SETTINGS);
            PendingIntent pendingIntent = PendingIntent.getActivity(
                    context, 0, wifiSettingsIntent, 
                    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
            
            // 构建通知
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle("WiFi已关闭")
                    .setContentText("点击打开WiFi设置")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true);
            
            // 显示通知
            NotificationManager notificationManager = 
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(NOTIFICATION_ID, builder.build());
            
            Log.d(TAG, "WiFi通知已显示");
        } catch (Exception e) {
            Log.e(TAG, "显示WiFi通知时出错: " + e.getMessage(), e);
        }
    }
    
    private void createNotificationChannel(Context context) {
        // 仅在Android 8.0及以上版本需要创建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "WiFi状态通知";
            String description = "当WiFi关闭时发送通知";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            
            NotificationManager notificationManager = 
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
    }
}
